﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ImportDataFromTxt
{
    public class BooksAndAuthorsImporter
    {
        public static void Main(string[] args)
        {
            string inputFile = "../../books.txt";
            ProcessData(inputFile);
        }

        private static void ProcessData(string inputFile)
        {
            StreamReader reader = new StreamReader(inputFile);
            using (reader)
            {
                reader.ReadLine();
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] info = line.Split('|');

                    string title = info[0].Trim();
                    string isbn = info[2].Trim();
                    decimal price = Decimal.Parse(info[3].Trim());
                    int amount = Int32.Parse(info[4].Trim());
                    string website = info[5].Trim();

                    Book book = GetBook(isbn, title, price, amount, website);
        //            RemoveAllAuthorsOfBook(book);
                    string[] authors = info[1].Split(new char[] { ',', ';' });
                    foreach (var authorName in authors)
                    {
                        Author author = GetAuthor(authorName.Trim());
                        AddRelation(book, author);
                    }
                }
            }
        }

        public static Author GetAuthor(string authorName)
        {
            BookstoreDataContext context = new BookstoreDataContext();
            Author author;
            using (context)
            {
                List<Author> authors =
                (from a in context.Authors
                 where a.Name == authorName
                 select a).ToList();

                if (authors.Count > 0)
                {
                    author = authors[0];
                }
                else
                {
                    author = new Author();
                    {
                        author.Name = authorName;
                    };
                    context.Authors.InsertOnSubmit(author);
                    context.SubmitChanges();
                }
            }
            return author;
        }

        public static Book GetBook(string isbn, string title, decimal price, int amount, string website)
        {
            isbn = isbn.Replace("-", String.Empty);
            BookstoreDataContext context = new BookstoreDataContext();
            using (context)
            {
                List<Book> books =
               (from b in context.Books
                where b.ISBN == isbn
                select b).ToList();
                Book book;
                if (books.Count > 0)
                {
                    book = books[0];
                }
                else
                {
                    book = new Book();
                    context.Books.InsertOnSubmit(book);
                }
                book.ISBN = isbn;
                book.Title = title;
                book.Price = price;
                if (book.Amount == null)
                {
                    book.Amount = 0;
                }
                book.Amount += amount;
                book.Website = website;
                
                context.SubmitChanges();
                return book;
            }
        }

        public static void AddRelation(Book book, Author author)
        {
            BookstoreDataContext context = new BookstoreDataContext();
            using (context)
            {
                List<BookToAuthor> relations =
              (from r in context.BookToAuthors
               where r.AuthorID == author.AuthorID &&
                    r.BookID == book.BookID
               select r).ToList();
                if (relations.Count == 0)
                {
                    BookToAuthor relation = new BookToAuthor();
                    {
                        relation.BookID = book.BookID;
                        relation.AuthorID = author.AuthorID;
                    };

                    context.BookToAuthors.InsertOnSubmit(relation);
                    context.SubmitChanges();
                }
            }
        }

        public static void RemoveAllAuthorsOfBook(Book book)
        {
            BookstoreDataContext context = new BookstoreDataContext();
            using (context)
            {
                List<BookToAuthor> relations =
              (from r in context.BookToAuthors
               where r.BookID == book.BookID
               select r).ToList();
                context.BookToAuthors.DeleteAllOnSubmit(relations);
            }
        }
    }
}
